package com.niuwa.p2p.action.interview;

import java.io.File;
import java.util.List;
import java.util.Map;

import org.apache.log4j.Logger;
import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;

import com.niuwa.p2p.action.JsonAction;
import com.niuwa.p2p.core.mvc.entity.JsonResult;
import com.niuwa.p2p.core.mvc.entity.PageBean;
import com.niuwa.p2p.entity.channel.LoanClaimAudit;
import com.niuwa.p2p.entity.channel.LoanRelativeFile;
import com.niuwa.p2p.entity.user.User;
import com.niuwa.p2p.opr.utils.SessionUtil;
import com.niuwa.p2p.service.filesystem.FileSystem;
import com.niuwa.p2p.service.interview.IInterviewService;
import com.niuwa.p2p.vo.interview.InterviewVo;

/**
 * 面签管理action
 * 
 * @author houdianbo
 * @Date 2015-04-23
 */
@Controller
public class InterviewAction extends JsonAction {
	private static final long serialVersionUID = 2997918077962934778L;
	private static Logger logger = Logger.getLogger(InterviewAction.class);
	@Autowired
	private IInterviewService interviewService;
	@Autowired
	private FileSystem fileSystem;

	private String projectNo;
	private List<LoanRelativeFile> lrfs;
	private String pdfPath;

	/**
	 * 跳转至面签列表页面
	 * 
	 * @return
	 */
	@Action(value = "gotoInterviewManage", results = { @Result(location = "interviewManage.jsp", name = SUCCESS) })
	public String gotoInterviewManage() {
		logger.info("gotoInterviewManage action execute success");
		return SUCCESS;
	}

	/**
	 * 面签列表
	 * 
	 * @return
	 * @throws Exception
	 */
	@Action(value = "searchInterviewList")
	public String searchInterviewList() throws Exception {
		setObjectReSult(queryPagination());
		logger.info("searchInterviewList action execute success");
		return NONE;
	}

	/**
	 * 面签审核
	 * 
	 * @return
	 * @throws Exception
	 */
	@Action(value = "viewAudit")
	public String viewAudit() throws Exception {
		Map<String, Object> paramMap = getParamMap();
		User user = SessionUtil.getSessionUser();
		boolean result = true;
		String msg = "操作成功！";
		try {
			result = this.interviewService.updateViewAuditStatus(paramMap,
					user.getId(), user.getUserName());
		} catch (Exception e) {
			e.printStackTrace();
			msg = "操作异常，请联系后台管理员。";
		}
		setResult(result, msg, null);
		return NONE;
	}

	/**
	 * 面签协议列表
	 * 
	 * @return
	 * @throws Exception
	 */
	@Action(value = "gotoInterviewDetails")
	public String gotoInterviewDetails() throws Exception {
		lrfs = this.interviewService.getLoanRelativeFile(getProjectNo());
		return "interviewDetails";

	}

	/**
	 * 分页查询
	 * 
	 * @return Pagination<InterviewVo>
	 */
	private PageBean<InterviewVo> queryPagination() {
		// 获取查询参数
		Map<String, Object> paramMap = getParamMap();
		logger.debug(paramMap);
		// 执行查询操作
		PageBean<InterviewVo> pagination = interviewService
				.selectInterviewByParam(paramMap);
		return pagination;
	}

	/**
	 * 获取面签拒绝的原因
	 * 
	 * @return
	 * @throws Exception
	 */
	@Action(value = "gotoLoanClaimAuditInfo")
	public String gotoLoanClaimAuditInfo() throws Exception {
		Map<String, Object> paramMap = getParamMap();
		LoanClaimAudit result = this.interviewService
				.getLoanClaimAudit(paramMap);
		getRequest().setAttribute("success", result != null);
		getRequest().setAttribute("record", result);
		return "viewAuditInfo";
	}
	/**
	 * 显示 PDF文件
	 * @return
	 * @throws Exception
	 */
	@Action(value = "viewPdfFile")
	public String viewPdfFile() throws Exception {
		String fileID = getRequest().getParameter("fileID");
		//File f = fileSystem.getFile(fileID);
		File f = fileSystem.getFile(fileID);
		setPdfPath(f.getAbsolutePath());
		return "viewInterviewPdf";
	}
	
	public void setInterviewService(IInterviewService interviewService) {
		this.interviewService = interviewService;
	}

	public String getProjectNo() {
		return projectNo;
	}

	public void setProjectNo(String projectNo) {
		this.projectNo = projectNo;
	}

	public List<LoanRelativeFile> getLrfs() {
		return lrfs;
	}

	public String getPdfPath() {
		return pdfPath;
	}

	public void setPdfPath(String pdfPath) {
		this.pdfPath = pdfPath;
	}
	
}
